import { Menu, message } from 'antd';
import React from 'react';
import { useHistory } from 'react-router';
import { Link } from 'react-router-dom';
import { IconFonts } from '../../../utils/Iconfont';
import { fetchRequest } from '../../../utils/Request';
import { url } from '../../../utils/url';
import { useRequest } from '@umijs/hooks';
import { useDispatch, useSelector } from 'react-redux';
import homepageType from '../homepageType';
import './style.css'

const { SubMenu, Item } = Menu


export default function SliderLayout() {


    //线上公告审核数量获取
    const tableData = useSelector((state: any) => {
        return state.annoManage
    })

    const history = useHistory();

    const getMenu = useRequest(getUserMenu, {
        loadingDelay: 200
    })

    const dispatch = useDispatch()

    async function getUserMenu() {
        try {
            const json = await fetchRequest(url.GET_USER_MENU, "");
            dispatch({ type: homepageType.GET_USER_MENU, payload: json.data })
        } catch (error: any) {
            message.destroy();
            message.error(error.message);
        }
    }

    const menu = useSelector((state: any) => {
        return state.homeManage.menu
    })


    const pathname = history.location.pathname
    const index = pathname.lastIndexOf('/')

    const defaultOpenKeys = pathname.substr(0, index)

    const [openKeys, setOpenKeys] = React.useState<any>([defaultOpenKeys]);
    const [MenuItemKes, setMenuItemKes] = React.useState<any>('');

    const onOpenChange = (keys: any) => {
        setOpenKeys(keys[keys.length - 1] ? [keys[keys.length - 1]] : [])

    };
    const onClick = (item: any) => {
        if (item.key === '/homepage/studentCardRecordIn/studentCardRecordIn' || item.key === '/homepage/videoSurveillance/videoSurveillance') {
            setMenuItemKes(item.key)
            setOpenKeys([])
        } else setMenuItemKes('')
    }
    return (
        <Menu mode="inline" theme="dark" selectedKeys={[history.location.pathname]}
            openKeys={openKeys}
            onOpenChange={onOpenChange}
            triggerSubMenuAction={'click'} 
            onClick={onClick}
        >

            <SubMenu
                className={typeof menu.studentManage === "undefined" || menu.studentManage.length === 0 ? "hide" : "show"}
                key="/homepage/entranceManage" title={<span><IconFonts type="icon-icon-renyuanguanli"></IconFonts>
                    <span>人员管理</span></span>}>{/*原学生管理*/}
                {/*<Item*/}
                {/*    className={typeof menu.studentManage !== "undefined"*/}
                {/*    && menu.studentManage.indexOf("personApproval") !== -1 ? "show" : "hide"}*/}
                {/*    key="/homepage/entranceManage/registerApproval">注册审核*/}
                {/*    <Link to="/homepage/entranceManage/registerApproval"/></Item>*/}

                {/*<Item*/}
                {/*    className={typeof menu.studentManage !== "undefined"*/}
                {/*    && menu.studentManage.indexOf("accCardApproval") !== -1 ? "show" : "hide"}*/}
                {/*    key="/homepage/entranceManage/entranceApproval">门禁审核*/}
                {/*    <Link to="/homepage/entranceManage/entranceApproval"/></Item>*/}

                <Item
                    className={typeof menu.studentManage !== "undefined" &&
                        menu.studentManage.indexOf("accCardManage") !== -1 ? "show" : "hide"}
                    key="/homepage/entranceManage/studentaccessManage">人员门禁管理
                    <Link to="/homepage/entranceManage/studentaccessManage" /></Item>{/*原学生门禁管理*/}

                <Item
                    className={typeof menu.studentManage !== "undefined" &&
                        menu.studentManage.indexOf("personManage") !== -1 ? "show" : "hide"}
                    key="/homepage/entranceManage/studentinformationManage">人员信息管理
                    <Link to="/homepage/entranceManage/studentinformationManage" /></Item>{/*原学生信息管理*/}
                <Item className={typeof menu.studentManage !== "undefined" &&
                    menu.studentManage.indexOf("passHisManage") !== -1 ? "show" : "hide"}
                    key="/homepage/entranceManage/accessRecord">
                    进出记录
                    <Link to="/homepage/entranceManage/accessRecord" />
                </Item>
                <Item key="/homepage/entranceManage/record"
                    className={typeof menu.studentManage !== "undefined" &&
                        menu.studentManage.indexOf("visitorManage") !== -1 ? "show" : "hide"}>
                    访客记录管理
                    <Link to="/homepage/entranceManage/record" />
                </Item>
                <Item key="/homepage/entranceManage/visitorSreview"
                    className={typeof menu.studentManage !== "undefined" &&
                        menu.studentManage.indexOf("visitorSreview") !== -1 ? "show" : "hide"}>
                    访客审核记录管理
                    <Link to="/homepage/entranceManage/visitorSreview" />
                </Item>
                <Item key="/homepage/entranceManage/leaves"
                    className={typeof menu.studentManage !== "undefined" &&
                        menu.studentManage.indexOf("leaveManage") !== -1 ? "show" : "hide"}>
                    请假记录管理
                    <Link to="/homepage/entranceManage/leaves" />
                </Item>
                <Item key="/homepage/entranceManage/curriculum"
                    className={typeof menu.studentManage !== "undefined" &&
                        menu.studentManage.indexOf("stucardSilentManage") !== -1 ? "show" : "hide"}>
                    智能学生证免打扰
                    <Link to="/homepage/entranceManage/curriculum" />
                </Item>
            </SubMenu>

            <SubMenu
                className={typeof menu.epidemicManage === "undefined" || menu.epidemicManage.length === 0 ? "hide" : "show"}
                key="/homepage/epidemicsituation" title={<span><IconFonts type="icon-icon_yiqing"></IconFonts>
                    <span>疫情防控</span></span>}>
                <Item
                    // className={typeof menu.dataAnalysisManage !== "undefined"
                    //     && menu.dataAnalysisManage.indexOf("personApproval") !== -1 ? "show" : "hide"}
                    key="/homepage/epidemicsituation/personnel">人员统计
                    <Link to="/homepage/epidemicsituation/personnel" /></Item>

                {/* <Item
                    // className={typeof menu.dataAnalysisManage !== "undefined"
                    //     && menu.dataAnalysisManage.indexOf("accCardApproval") !== -1 ? "show" : "hide"}
                    key="/homepage/epidemicsituation/visitor">访客记录
                    <Link to="/homepage/epidemicsituation/visitor" /></Item> */}

                <Item
                    // className={typeof menu.dataAnalysisManage !== "undefined" &&
                    //     menu.dataAnalysisManage.indexOf("accCardManage") !== -1 ? "show" : "hide"}
                    key="/homepage/epidemicsituation/abnormal">异常人员
                    <Link to="/homepage/epidemicsituation/abnormal" /></Item>
                {/* <Item
                    key="/homepage/epidemicsituation/identitycard">身份证异常人员
                    <Link to="/homepage/epidemicsituation/identitycard" /></Item> */}
            </SubMenu>

            <SubMenu
                className={typeof menu.sysManage === "undefined" ||
                    menu.sysManage.length === 0 ? "hide" : "show"}
                key="/homepage/systemManage" title={<span>
                    <IconFonts type="icon-icon-xitongguanli"></IconFonts>
                    <span>系统管理</span>
                </span>}>
                <Item key="/homepage/systemManage/deptManage"
                    className={typeof menu.sysManage !== "undefined"
                        && menu.sysManage.indexOf("deptManage") !== -1 ? "show" : "hide"}
                >
                    组织管理
                    <Link to="/homepage/systemManage/deptManage" />
                </Item>
                <Item
                    className={typeof menu.sysManage !== "undefined"
                        && menu.sysManage.indexOf("staffManage") !== -1 ? "show" : "hide"}
                    key="/homepage/systemManage/personManage">
                    工号管理
                    <Link to="/homepage/systemManage/personManage" />
                </Item>
                {/*<Item key="/homepage/systemManage/equipment"*/}
                {/*      className={typeof menu.sysManage !== "undefined"*/}
                {/*      && menu.sysManage.indexOf("deviceManage") !== -1 ? "show" : "hide"}*/}
                {/*>*/}
                {/*    设备管理*/}
                {/*    <Link to="/homepage/systemManage/equipment"/>*/}
                {/*</Item>*/}

                <Item key="/homepage/systemManage/equipmentManipulate"
                    className={typeof menu.sysManage !== "undefined"
                        && menu.sysManage.indexOf("deviceManipulateManage") !== -1 ? "show" : "hide"}
                >
                    设备操作
                    <Link to="/homepage/systemManage/equipmentManipulate" />
                </Item>

                <Item key="/homepage/systemManage/addressmanage"

                    className={typeof menu.sysManage !== "undefined"
                        && menu.sysManage.indexOf("addressManage") !== -1 ? "show" : "hide"}

                >地址管理<Link to="/homepage/systemManage/addressmanage" />
                </Item>

                <Item key="/homepage/systemManage/campusmanage"

                    className={typeof menu.sysManage !== "undefined"
                        && menu.sysManage.indexOf("collegeAreaManage") !== -1 ? "show" : "hide"}

                >
                    校区管理
                    <Link to="/homepage/systemManage/campusmanage" />
                </Item>


                <Item key="/homepage/systemManage/schooldetail"

                    className={typeof menu.sysManage !== "undefined"
                        && menu.sysManage.indexOf("collegeDataReport") !== -1 ? "show" : "hide"}

                >
                    校园周报 
                    <Link to="/homepage/systemManage/schooldetail" />
                </Item>

                <Item key="/homepage/systemManage/schoolManage"
                    className={typeof menu.sysManage !== "undefined" &&
                        menu.sysManage.indexOf("sysOrzManage") !== -1 ? "show" : "hide"}>
                    学校管理
                    <Link to="/homepage/systemManage/schoolManage" />
                </Item>
                <Item key="/homepage/systemManage/lightUpSchool"
                    className={typeof menu.sysManage !== "undefined" &&
                        menu.sysManage.indexOf("lightUpSchool") !== -1 ? "show" : "hide"}>
                    点亮学校
                    <Link to="/homepage/systemManage/lightUpSchool" />
                </Item>
                <Item key="/homepage/systemManage/ruleManage"
                    className={typeof menu.sysManage !== "undefined" &&
                        menu.sysManage.indexOf("accessCardRuleManage") !== -1 ? "show" : "hide"}>
                    门禁规则管理
                    <Link to="/homepage/systemManage/ruleManage" />
                </Item>
                <Item key="/homepage/systemManage/StudentCardEenew"
                    className={typeof menu.sysManage !== "undefined" &&
                        menu.sysManage.indexOf("whiteCardReplaceManage") !== -1 ? "show" : "hide"}
                        >
                    学生证批量换新
                    <Link to="/homepage/systemManage/StudentCardEenew" />
                </Item>
                <Item key="/homepage/systemManage/studentsOperations"
                    className={typeof menu.sysManage !== "undefined" &&
                        menu.sysManage.indexOf("stucardSupportManage") !== -1 ? "show" : "hide"}>
                    学生证运维管理
                    <Link to="/homepage/systemManage/studentsOperations" />
                </Item>


                <Item key="/homepage/systemManage/bulkImport"
                    className={typeof menu.sysManage !== "undefined" &&
                        menu.sysManage.indexOf("batchAccessManage") !== -1 ? "show" : "hide"}>
                    学生门禁权限批量开通
                    <Link to="/homepage/systemManage/bulkImport" />
                </Item>

            </SubMenu>
            <SubMenu
                className={typeof menu.dataAnalysisManage === "undefined" ||
                    menu.dataAnalysisManage.length === 0 ? "hide" : "show"}
                key="/homepage/dataAnalysis" title={<span>
                    <IconFonts type="icon-icon-shujufenxi"></IconFonts>
                    <span>数据分析/统计</span>
                </span>}>
                <Item key="/homepage/dataAnalysis/schoolAnalysis"
                    className={typeof menu.dataAnalysisManage !== "undefined" &&
                        menu.dataAnalysisManage.indexOf("dataAnalysisManage") !== -1 ? "show" : "hide"}>
                    在校情况统计
                    <Link to="/homepage/dataAnalysis/schoolAnalysis" />
                </Item>
                <Item key="/homepage/dataAnalysis/dormAnalysis"
                    className={typeof menu.dataAnalysisManage !== "undefined" &&
                        menu.dataAnalysisManage.indexOf("dataAnalysisManage") !== -1 ? "show" : "hide"}>
                    在寝情况统计
                    <Link to="/homepage/dataAnalysis/dormAnalysis" />
                </Item>
                <Item key="/homepage/dataAnalysis/videoAttendance"
                    className={typeof menu.dataAnalysisManage !== "undefined" &&
                        menu.dataAnalysisManage.indexOf("dataAnalysisManage") !== -1 ? "show" : "hide"}>
                    视频考勤统计
                    <Link to="/homepage/dataAnalysis/videoAttendance" />
                </Item>
            </SubMenu>

            <Menu.Item
                className={[typeof menu.studentCardManage === "undefined" ||
                    menu.studentCardManage.length === 0 ? "hide" : "show",
                MenuItemKes === '/homepage/studentCardRecordIn/studentCardRecordIn' || pathname === '/homepage/studentCardRecordIn/studentCardRecordIn' ? 'MenuName' : '']
                    .join(' ')}
                key={"/homepage/studentCardRecordIn/studentCardRecordIn"}
            >
                <span><IconFonts type="icon-icon-zhengjian"></IconFonts><span>学生卡入库</span></span>
                <Link to={"/homepage/studentCardRecordIn/studentCardRecordIn"} />
            </Menu.Item>

            <Menu.Item
                className={[typeof menu.monitorManage === "undefined" ||
                    menu.monitorManage.length === 0 ? "hide" : "show",
                MenuItemKes === '/homepage/videoSurveillance/videoSurveillance' || pathname === '/homepage/videoSurveillance/videoSurveillance' ? 'MenuName' : ''
                ].join(' ')}
                key={"/homepage/videoSurveillance/videoSurveillance"}
            >
                <span><IconFonts type="icon-icon-shipinjiankong"></IconFonts><span>视频监控</span></span>
                <Link to={"/homepage/videoSurveillance/videoSurveillance"} />
            </Menu.Item>

            <SubMenu
                className={typeof menu.interactManage === "undefined" ||
                    menu.interactManage.length === 0 ? "hide" : "show"}
                key="/homepage/interactManage" title={<span>
                    <IconFonts type="icon-icon-jiaxiaohudong"></IconFonts>
                    <span>家校互动</span>
                </span>}>
                <Item key="/homepage/interactManage/achievement"
                    className={typeof menu.interactManage !== "undefined" &&
                        menu.interactManage.indexOf("scoreManage") !== -1 ? "show" : "hide"}>
                    成绩管理
                    <Link to="/homepage/interactManage/achievement" />
                </Item>

                <Item key="/homepage/interactManage/achievementIssue"
                    className={typeof menu.interactManage !== "undefined" &&
                        menu.interactManage.indexOf("scoreReport") !== -1 ? "show" : "hide"}>
                    成绩发布
                    <Link to="/homepage/interactManage/achievementIssue" />
                </Item>

                <Item key="/homepage/interactManage/releaseInformation"
                    className={typeof menu.interactManage !== "undefined" &&
                        menu.interactManage.indexOf("infoReport") !== -1 ? "show" : "hide"}>
                    信息发布
                    <Link to="/homepage/interactManage/releaseInformation" />
                </Item>

                <Item key="/homepage/interactManage/releaseInformationAudit"
                    className={typeof menu.interactManage !== "undefined" &&
                        menu.interactManage.indexOf("infoAudit") !== -1 ? "show" : "hide"}>
                    信息发布审核
                    <Link to="/homepage/interactManage/releaseInformationAudit" />
                </Item>
            </SubMenu>

            <SubMenu
                className={typeof menu.deviceManage === "undefined" || menu.deviceManage.length === 0 ? "hide" : "show"}
                key="/homepage/terminal" title={<span><IconFonts type="icon-icon-zhongduan"></IconFonts>
                    <span>终端管理</span></span>}>
                <Item key="/homepage/terminal/equipment"
                    className={typeof menu.deviceManage !== "undefined"
                        && menu.deviceManage.indexOf("doorDeviceManage") !== -1 ? "show" : "hide"}
                >
                    门禁设备管理
                    <Link to="/homepage/terminal/equipment" />
                </Item>

                <Item
                    className={typeof menu.deviceManage !== "undefined"
                        && menu.deviceManage.indexOf("cameraDeviceManage") !== -1 ? "show" : "hide"}
                    key="/homepage/terminal/videoterminal">视频设备管理
                    <Link to="/homepage/terminal/videoterminal" /></Item>

                <Item
                    className={typeof menu.deviceManage !== "undefined" &&
                        menu.deviceManage.indexOf("scanDeviceManage") !== -1 ? "show" : "hide"}
                    key="/homepage/terminal/scanterminal">扫码设备管理
                    <Link to="/homepage/terminal/scanterminal" /></Item>
            </SubMenu>
            {/*<Menu.Item*/}
            {/*    className={typeof menu.channelUserManage === "undefined" ||*/}
            {/*    menu.channelUserManage.length === 0 ? "hide" : "show"}*/}
            {/*    key={"/homepage/studentCardRecordIn/studentCardRecordIn"}*/}
            {/*>*/}
            {/*    <span><ContactsOutlined/><span>渠道人员管理</span></span>*/}
            {/*    <Link to={"/homepage/ChannelStaffManage/ChannelStaffManage"}/>*/}
            {/*</Menu.Item>*/}
        </Menu>
    )
}
